﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MvcEngine.Core.Services;
using System.Web.Mvc;
using MvcEngine.Mvc.Extensions;
using MvcEngine.Core;
using MvcEngine.Core.Registry;
using MvcEngine.Blog.Filters;
using MvcEngine.Mvc.Filters;
using MvcEngine.Mvc;
using MvcEngine.Blog.Models.Entities;
using MvcEngine.Blog.ModelBinders;
using MvcEngine.Blog.StarterSite.Controllers;
using MvcEngine.Mvc.Services;

namespace MvcEngine.Blog.Services
{
    public class TagControlService : ControlServiceBase
    {
        public override void RegisterBackofficeRoutes(AreaRegistrationContext context)
        {
            context.MapMvcEngineRoute(
                "BackOffice_BlogTagList",
                "BackOffice/Blog/{blogName}/Tags",
                new { controller = "BlogTag", action = "List" },
                new string[] { "MvcEngine.Blog.BackOffice.Controllers" }
            );

            context.MapMvcEngineRoute(
                "BackOffice_BlogTagPaging",
                "BackOffice/Blog/{blogName}/Tags/Page{pageNumber}",
                new { controller = "BlogTag", action = "List", pageNumber = "0" },
                new string[] { "MvcEngine.Blog.BackOffice.Controllers" }
            );

            context.MapMvcEngineRoute(
                "BackOffice_BlogNewTag",
                "BackOffice/Blog/{blogName}/Tag/New",
                new { controller = "BlogTag", action = "Edit", id = "" },
                new string[] { "MvcEngine.Blog.BackOffice.Controllers" }
            );

            context.MapMvcEngineRoute(
                "BackOffice_BlogEditTag",
                "BackOffice/Blog/{blogName}/Tag/Edit/{id}",
                new { controller = "BlogTag", action = "Edit" },
                new string[] { "MvcEngine.Blog.BackOffice.Controllers" }
            );

            context.MapMvcEngineRoute(
                "BackOffice_BlogDeleteTag",
                "BackOffice/Blog/{blogName}/Tag/Delete/{id}",
                new { controller = "BlogTag", action = "Delete" },
                new string[] { "MvcEngine.Blog.BackOffice.Controllers" }
            );
        }

        public override void RegisterActionFilters(Core.Registry.IActionFilterRegistry registry)
        {
            //--------------------------------------------------------------------------------------
            //------------------------------Common filters------------------------------------------
            //--------------------------------------------------------------------------------------
            ControllerCriteria backOfficeCommonCriteria = new ControllerCriteria();
            backOfficeCommonCriteria.AddController(typeof(MvcEngine.Blog.BackOffice.Controllers.BlogTagController));
            registry.Add(new[] { backOfficeCommonCriteria }, typeof(MvcEngine.Blog.BackOffice.Filters.BlogActionFilter));

            ControllerCriteria starterSiteCommonCriteria = new ControllerCriteria();
            starterSiteCommonCriteria.AddController(typeof(MvcEngine.Blog.StarterSite.Controllers.BlogTagController));
            registry.Add(new[] { starterSiteCommonCriteria }, typeof(MvcEngine.Blog.StarterSite.Filters.BlogActionFilter));

            //--------------------------------------------------------------------------------------
            //------------------------------End of common filters-----------------------------------
            //--------------------------------------------------------------------------------------

            ControllerCriteria moduleCriteria = new ControllerCriteria();
            moduleCriteria.AddController(typeof(MvcEngine.Blog.BackOffice.Controllers.BlogTagController));
            registry.Add(new[] { moduleCriteria }, new ModuleActionFilter());

            //--------------------------------------------------------------------------------------
            //---------------------------------Permissions------------------------------------------
            //--------------------------------------------------------------------------------------
            ControllerCriteria viewCriteria = new ControllerCriteria();
            viewCriteria.AddController(typeof(MvcEngine.Blog.StarterSite.Controllers.BlogTagController));

            ControllerCriteria editCriteria = new ControllerCriteria();
            editCriteria.AddController(typeof(MvcEngine.Blog.BackOffice.Controllers.BlogTagController));
            //--------------------------------------------------------------------------------------
            //---------------------------------End of permissions----------------------------------
            //--------------------------------------------------------------------------------------

            registry.Add(new[] { editCriteria }, new RolePermissionAttribute(Constants.Roles.Administrators));
            registry.Add(new[] { viewCriteria }, new ModulePermissionAttribute(PermissionTypes.View));
        }

        public override void RegisterModelBinders(ModelBinderDictionary binders)
        {
            binders[typeof(BlogTagBase)] = new BlogTagModelBinder();    
        }

        public override Type GetControllerType()
        {
            return typeof(BlogTagController);
        }

        public override string GetDefaultAction()
        {
            return "Index";
        }
    }
}
